package com.irs.controller;

import com.irs.pojo.BuildingHouse;
import com.irs.pojo.BuildingMusic;
import com.irs.pojo.Result;
import com.irs.service.BuildingMusicService;
import com.irs.util.ResultUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.util.List;


@CrossOrigin("*")
@Controller
public class BuildingMusicController {
    @Autowired
    private BuildingMusicService buildingMusicService;
    //跳转到music下

    @GetMapping("/music/list")
    public String tobuildingMusic(){
        //跳转到buildHouse.jsp
        return "page/audio/buildingMusic";
    }
    /*
    @GetMapping("/audio/musci/list")
    @ResponseBody
    //查询
    public Result getAll(){
        Result result = new Result();
        List<BuildingMusic> musics = buildingMusicService.getAll();
        if (musics != null) {
            result.setItem(musics);
        } else {
            //状态码
            result.setStatus("500");
            result.setMessage("error");
        }
        return result;
        //返回JSON
    }*/

    //查
    @GetMapping("/audio/musci/list")
    @ResponseBody
    public Object buildingMusic(){
        List<BuildingMusic> buildingMusicList = buildingMusicService.select(new BuildingMusic());
        //返回JSON
        return  ResultUtil.ok(buildingMusicList, buildingMusicList.size());
    }

    @GetMapping("/audio/musci/del")
    @ResponseBody
    //删除
    public Result del(String[] idlist) {
        Result result = new Result();
        try {
            String s = buildingMusicService.del(idlist);
            if (s.equals("music删除成功")){
                return result;
            }else {
                result.setMessage("请刷新页面");
                return result;
            }
        }catch (Exception e){
            e.printStackTrace();
            result.setMessage("error");
            result.setStatus("500");
        }
        return result;
    }

    @GetMapping("/audio/musci/up")
    @ResponseBody
    //上传
    public Result up(MultipartFile file, HttpServletRequest request) {
        //进行文件上传操作
        Result result = new Result();
        try {
            buildingMusicService.up(file ,request);
            //拿到上传文件的名字
            String s = file.getOriginalFilename();
            result.setMessage(s);
            return  result;

        }catch (Exception e){
            e.printStackTrace();
            result.setMessage("文件操作异常");
            return result;
        }
    }


    @GetMapping("/audio/musci//add")
    @ResponseBody
    public Result add(BuildingMusic music){
        Result result = new Result();
        try {
            buildingMusicService.add(music);
            return result;
        }catch (Exception e){
            e.printStackTrace();
            result.setMessage("musicerror");
            return result;
        }
    }

    //更新
    @GetMapping("/audio/music/update")
    @ResponseBody
    public Result update(BuildingMusic music) {
        Result result = new Result();
        try {
            buildingMusicService.update(music);
            return result;

        } catch (Exception e) {
            e.printStackTrace();

            return result;
        }

    }

}
